package by.vkhalko.asamo.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.vkhalko.asamo.dataaccess.OrderDao;
import by.vkhalko.asamo.datamodel.Company;
import by.vkhalko.asamo.datamodel.Order;
import by.vkhalko.asamo.datamodel.Person;
import by.vkhalko.asamo.datamodel.Status;
import by.vkhalko.asamo.services.OrderService;

@Service
public class OrderServiceImpl implements OrderService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(OrderServiceImpl.class);

	@Inject
	private OrderDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of OrderService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Order get(Long id) {
		Order entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Order order) {
		if (order.getId() == null) {
			LOGGER.debug("Save new: {}", order);
			dao.insert(order);
		} else {
			LOGGER.debug("Update: {}", order);
			dao.update(order);
		}
	}

	@Override
	public void delete(Order order) {
		LOGGER.debug("Remove: {}", order);
		dao.delete(order.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all orders");
		dao.deleteAll();
	}

	@Override
	public List<Order> getAllOrders() {
		return dao.getAllOrders();
	}

	@Override
	public List<Order> getAllByCompany(Company company) {
		return dao.getAllByCompany(company);
	}

	@Override
	public List<Order> getAllByPerson(Person person) {
		return dao.getAllByPerson(person);
	}

	@Override
	public List<Order> getAllByStatus(Status status) {
		return dao.getAllByStatus(status);
	}

}
